package com.lovo.controller;

import com.lovo.util.SnowIdUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class UploadController {

    @PostMapping("upload")
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {

        String winDir="d:/JAVA/img/";
        String linuxDir="/mnt/img2/";
        if(!winDir.isEmpty()){
            File f=new File(winDir);
            f.mkdirs();
        }
        if(!linuxDir.isEmpty()){
            File f=new File(linuxDir);
            f.mkdirs();
        }
        int point=file.getOriginalFilename().lastIndexOf(".");
        String suffix=    file.getOriginalFilename().substring(point,file.getOriginalFilename().length());
        String fileName=  "-"+ SnowIdUtils.uniqueLongHex()+suffix;
        String os = System.getProperty("os.name");
        File fileNew=null;
        //Windows操作系统
        if (os != null && os.toLowerCase().startsWith("windows")) {
            fileNew=new File(winDir+"/"+fileName);
            file.transferTo(fileNew);
            //  Tosmallerpic(winDir,winDir,fileName,0,0,(float)0.9);
        } else if (os != null && os.toLowerCase().startsWith("linux")) {//Linux操作系统
            fileNew=new File(linuxDir+"/"+fileName);
            file.transferTo(fileNew);
            // Tosmallerpic(linuxDir,linuxDir,fileName,0,0,(float)0.9);

        }

        return  fileName;
    }
}
